using AdminApiTemplate.Aggregates.Login;
using AdminApiTemplate.Api.Controllers.BaseControllers;
using AdminApiTemplate.Api.Enums;
using AdminApiTemplate.Api.Helpers;
using AdminApiTemplate.Api.ViewModels.Login;
using AdminApiTemplate.Cache;
using AdminApiTemplate.Common.Exceptions;
using Microsoft.AspNetCore.Mvc;

namespace AdminApiTemplate.Api.Controllers
{
    /// <summary>
    /// 登录
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class LoginController : FreeAccessController
    {
        private readonly LoginComposite _loginComposite;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="loginComposite"></param>
        public LoginController(
            LoginComposite loginComposite)
        {
            _loginComposite = loginComposite;
        }

        /// <summary>
        /// 账号密码登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ByPass")]
        //[LocalApi]
        public IActionResult ByPass([FromBody] ByPassRequestModel request)
        {
            return new WebHelper(HttpContext, request).Execute(SysUserTypeEnum.None, string.Empty, () =>
            {
                return _loginComposite.ByPassAsync(request).Result;
            });
        }

        /// <summary>
        /// 企业微信登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("ByWorkWx")]
        public IActionResult ByWorkWx([FromBody] ByWorkWxRequestModel request)
        {
            return new WebHelper(HttpContext, request).Execute(SysUserTypeEnum.None, string.Empty, () =>
            {
                if (string.IsNullOrEmpty(request.App))
                {
                    request.App = "AMZ";
                }

                var appId = ConfigHelper.GetSysConfig<string>("WorkWx:AppId", "");
                var appSecret = ConfigHelper.GetSysConfig<string>($"WorkWx:AppSecret:{request.App}", "");
                if (string.IsNullOrEmpty(appId) || string.IsNullOrEmpty(appSecret))
                {
                    throw new WithCodeException((int)ErrorCodeEnum.CodeError, "请联系管理员进行企业微信相关配置", $"请配置{request.App}的企业微信AppId和AppSecret");
                }
                return _loginComposite.ByWorkWxAsync(request, appId, appSecret).Result;
            });
        }

        /// <summary>
        /// 企业微信登录配置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetWorkWxConfig")]
        public IActionResult GetWorkWxConfig(string app = "AMZ")
        {
            return new WebHelper(HttpContext, null, false).Execute(SysUserTypeEnum.None, string.Empty, () =>
            {
                var appId = ConfigHelper.GetSysConfig<string>("WorkWx:AppId", "");
                var agentId = ConfigHelper.GetSysConfig<string>($"WorkWx:AgentId:{app}", "");
                if (string.IsNullOrEmpty(appId) || string.IsNullOrEmpty(agentId))
                {
                    throw new WithCodeException((int)ErrorCodeEnum.CodeError, "请联系管理员进行企业微信相关配置", $"请配置{app}的企业微信AppId和AgentId");
                }
                return new GetWorkWxConfigViewModel
                {
                    AppId = appId,
                    AgentId = agentId
                };
            });
        }
    }
}
